﻿Imports System.IO
Imports System.Xml

Public Class EditConnectionString
    Dim oCurrentConnection As CustomConnection
    Dim IsAdding As Boolean = False
    Dim IsModifying As Boolean = False

    Sub New()
        InitializeComponent()
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnnuler.Click
        If IsAdding Or IsModifying Then
            ReloadList()
            ConList.Enabled = True
            txtNomCon.Enabled = False
            txtdbname.Enabled = False
            txtdbpass.Enabled = False
            txtNom.Enabled = False
            txtusername.Enabled = False
            btnAdd.Visible = True
            btnEdit.Visible = True
            BtnDel.Visible = True
            IsAdding = False
            IsModifying = False
        Else
            Me.Close()
        End If

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim XmlDoc As XmlDocument = New XmlDocument()

        XmlDoc.Load("Config.ini")

        If Not IsAdding Then

            For Each item As XmlElement In XmlDoc.GetElementsByTagName("Connection")
                Dim oConection As New CustomConnection

                With item
                    If .GetElementsByTagName("ConnectionName")(0).InnerText = txtNomCon.Text Then
                        .GetElementsByTagName("UserName")(0).InnerText = txtusername.Text
                        .GetElementsByTagName("UserPass")(0).InnerText = txtdbpass.Text
                        .GetElementsByTagName("ip")(0).InnerText = txtNom.Text
                        .GetElementsByTagName("databasename")(0).InnerText = txtdbname.Text
                    End If
                End With
            Next

        Else
            Dim oList As List(Of XmlElement) = (From u As XmlElement In XmlDoc.GetElementsByTagName("Connection") Select u).ToList

            Dim oNewElement As XmlElement = XmlDoc.CreateElement("Connection")

            Dim oConName As XmlElement = XmlDoc.CreateElement("ConnectionName")
            Dim oUserName As XmlElement = XmlDoc.CreateElement("UserName")
            Dim oUserPass As XmlElement = XmlDoc.CreateElement("UserPass")
            Dim oip As XmlElement = XmlDoc.CreateElement("ip")
            Dim odatabasename As XmlElement = XmlDoc.CreateElement("databasename")
            Dim oIsDefault As XmlElement = XmlDoc.CreateElement("IsDefault")

            oConName.InnerText = txtNomCon.Text
            oUserName.InnerText = txtusername.Text
            oUserPass.InnerText = txtdbpass.Text
            oip.InnerText = txtNom.Text
            odatabasename.InnerText = txtdbname.Text

            oNewElement.AppendChild(oConName)
            oNewElement.AppendChild(oip)
            oNewElement.AppendChild(odatabasename)
            oNewElement.AppendChild(oUserName)
            oNewElement.AppendChild(oUserPass)
            oNewElement.AppendChild(oIsDefault)

            XmlDoc.DocumentElement.AppendChild(oNewElement)

        End If

        XmlDoc.Save("Config.ini")
        ReloadList()
        ConList.Enabled = True
        txtNomCon.Enabled = False
        txtdbname.Enabled = False
        txtdbpass.Enabled = False
        txtNom.Enabled = False
        txtusername.Enabled = False
        btnAdd.Visible = True
        btnEdit.Visible = True
        BtnDel.Visible = True
        IsAdding = False
        IsModifying = False
    End Sub


   

    Private Sub EditConnectionString_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        ReloadList()

    End Sub

    Private Sub ConList_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ConList.SelectedIndexChanged
        Dim oCustomCon As New CustomConnection

        oCustomCon = CType(ConList.SelectedValue, CustomConnection)

        SetValues(oCustomCon)


    End Sub

    Sub ReloadList()
        Dim oConnectionList As New List(Of CustomConnection)

        Dim XmlDoc As XmlDocument = New XmlDocument()

        XmlDoc.Load("Config.ini")
        For Each item As XmlElement In XmlDoc.GetElementsByTagName("Connection")
            Dim oConection As New CustomConnection
            With item
                oConection.UserConnection = .GetElementsByTagName("UserName")(0).InnerText
                oConection.PassConnection = .GetElementsByTagName("UserPass")(0).InnerText
                oConection.IPConnection = .GetElementsByTagName("ip")(0).InnerText
                oConection.NomConnection = .GetElementsByTagName("ConnectionName")(0).InnerText
                oConection.DataBaseName = .GetElementsByTagName("databasename")(0).InnerText
            End With
            oConnectionList.Add(oConection)
        Next

        ConList.DataSource = oConnectionList
        ConList.DisplayMember = "NomConnection"
    End Sub

    Sub SetValues(ByVal oCustomCon As CustomConnection)
        txtdbpass.Text = oCustomCon.PassConnection
        txtdbname.Text = oCustomCon.DataBaseName
        txtNomCon.Text = oCustomCon.NomConnection
        txtusername.Text = oCustomCon.UserConnection
        txtNom.Text = oCustomCon.IPConnection
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        IsAdding = True
        ConList.Enabled = False
        txtNomCon.Enabled = True
        txtdbname.Enabled = True
        txtdbpass.Enabled = True
        txtNom.Enabled = True
        txtNomCon.Enabled = True
        txtusername.Enabled = True
        btnEdit.Visible = False
        BtnDel.Visible = False
        oCurrentConnection = New CustomConnection
        SetValues(oCurrentConnection)
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        IsModifying = True
        ConList.Enabled = False
        txtdbname.Enabled = True
        txtdbpass.Enabled = True
        txtNom.Enabled = True
        txtusername.Enabled = True
        btnAdd.Visible = False
        BtnDel.Visible = False

    End Sub

    Private Sub BtnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDel.Click
        Dim XmlDoc As XmlDocument = New XmlDocument()

        XmlDoc.Load("Config.ini")

        Dim o As XmlElement = (From u As XmlElement In XmlDoc.GetElementsByTagName("ConnectionName") Where u.InnerText = CType(ConList.SelectedItem, CustomConnection).NomConnection Select u.ParentNode).FirstOrDefault

        XmlDoc.DocumentElement.RemoveChild(o)

        XmlDoc.Save("Config.ini")
        ReloadList()

    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

    End Sub
End Class

Public Class CustomConnection
    Private Nom As String
    Public Property NomConnection() As String
        Get
            Return IIf(IsDefault, "*" + Nom, Nom)
        End Get
        Set(ByVal value As String)
            Nom = value
        End Set
    End Property

    Private IP As String
    Public Property IPConnection() As String
        Get
            Return IP
        End Get
        Set(ByVal value As String)
            IP = value
        End Set
    End Property

    Private UserName As String
    Public Property UserConnection() As String
        Get
            Return UserName
        End Get
        Set(ByVal value As String)
            UserName = value
        End Set
    End Property

    Private UserPass As String
    Public Property PassConnection() As String
        Get
            Return UserPass
        End Get
        Set(ByVal value As String)
            UserPass = value
        End Set
    End Property

    Private DBName As String
    Public Property DataBaseName() As String
        Get
            Return DBName
        End Get
        Set(ByVal value As String)
            DBName = value
        End Set
    End Property


    Private IsDefault As Boolean
    Public Property ParDefault() As Boolean
        Get
            Return IsDefault
        End Get
        Set(ByVal value As Boolean)
            IsDefault = value
        End Set
    End Property




    Sub New()
        Me.IP = "0.0.0.0"
        Me.Nom = ""
        Me.UserName = ""
        Me.UserPass = ""
    End Sub

    Public Function getConnectionString(ByVal oCon As CustomConnection) As String
        Return "Data Source=" + oCon.IPConnection + ";Initial Catalog=" + oCon.DBName + ";Persist Security Info=True;User ID=" + oCon.UserName + ";Password=" + oCon.UserPass
    End Function

    Public Function GetList() As List(Of CustomConnection)
        Dim oConnectionList As New List(Of CustomConnection)

        Dim XmlDoc As XmlDocument = New XmlDocument()

        XmlDoc.Load("Config.ini")
        For Each item As XmlElement In XmlDoc.GetElementsByTagName("Connection")
            Dim oConection As New CustomConnection
            With item
                oConection.UserConnection = .GetElementsByTagName("UserName")(0).InnerText
                oConection.PassConnection = .GetElementsByTagName("UserPass")(0).InnerText
                oConection.IPConnection = .GetElementsByTagName("ip")(0).InnerText
                oConection.NomConnection = .GetElementsByTagName("ConnectionName")(0).InnerText
                oConection.DataBaseName = .GetElementsByTagName("databasename")(0).InnerText
            End With
            oConnectionList.Add(oConection)
        Next

        Return oConnectionList

    End Function

End Class